﻿namespace Cloudio.Web.UI.Designer
{
    using System;
    using System.Web;
    using Gaia.WebWidgets;
    using ASP = System.Web.UI.WebControls;
    using Editor;

    public class DesignerBase: Panel
    {
        private readonly EditorBase _editor = new EditorBase
                                                  {
                                                      ID = "pnlEditorddd",
                                                      Height = new ASP.Unit(350),
                                                      BorderStyle = ASP.BorderStyle.Dotted,
                                                      CssClass = "editor"
                                                  };

        private readonly Panel _pnlMarkup = new Panel {ID= "pnlMarkup", Height = new ASP.Unit(400), CssClass = "markup"};

        private readonly LinkButton _lnkBtnShowDesign = new LinkButton {ID = "lnkBtnShowDesign", Text = "Design", CssClass="editor-bottom-linkbtns"};
        private readonly LinkButton _lnkBtnShowSource = new LinkButton { ID = "lnkBtnShowSource", Text = "Source", CssClass = "editor-bottom-linkbtns" };
        private readonly LinkButton _lnkBtnResetMarkup = new LinkButton { ID = "lnkBtnResetMarkup", Text = "Reset", CssClass = "editor-bottom-linkbtns" };
        private readonly Label _lblMarkup = new Label {ID = "lblMarkup"};

        public event EventHandler<SmartTagEventArgs> SmartTagClick;

        private enum DesignerControlView
        {
            Design,
            Source
        }

        protected override void OnInit(EventArgs e)
        {
            //make markup editable
            _pnlMarkup.Attributes.Add("contenteditable", "true");

            _lnkBtnShowDesign.Click += _lnkBtnShowDesign_Click;
            _lnkBtnShowSource.Click += _lnkBtnShowSource_Click;
            _lnkBtnResetMarkup.Click += _lnkBtnResetMarkup_Click;

            _editor.SmartTagClick += _pnlEditor_SmartTagClick;

            base.OnInit(e);
        }

        void _pnlEditor_SmartTagClick(object sender, SmartTagEventArgs e)
        {
            //forward this call
            if (SmartTagClick != null)
                SmartTagClick(sender, e);
        }

        void _lnkBtnResetMarkup_Click(object sender, EventArgs e)
        {
            _editor.Reset();
            _editor.RefreshContent();
        }

        void _lnkBtnShowSource_Click(object sender, EventArgs e)
        {
            _lblMarkup.Text = HttpContext.Current.Server.HtmlEncode(_editor.Markup);
            ToggleDesignSource();
        }

        void _lnkBtnShowDesign_Click(object sender, EventArgs e)
        {
            //TODO implement saving here
            //save html
            //EditorBase.SaveToFile(HttpContext.Current.Server.HtmlDecode(_lblMarkup.Text));
            //_pnlEditor.RefreshContent();

            ToggleDesignSource();
        }

        protected override void OnLoad(EventArgs e)
        {
            EnsureChildControls();
            base.OnLoad(e);

            if (!Page.IsPostBack)
            {
                _lblMarkup.Text = HttpContext.Current.Server.HtmlEncode(_editor.Markup);
                CurrentDesignerControlView = DesignerControlView.Source;
            }
        }

        protected override void CreateChildControls()
        {
            _pnlMarkup.Controls.Add(_lblMarkup);

            Controls.Add(_editor);
            Controls.Add(_pnlMarkup);
            Controls.Add(_lnkBtnShowDesign);
            Controls.Add(_lnkBtnShowSource);
            Controls.Add(_lnkBtnResetMarkup);

            base.CreateChildControls();
        }

        private DesignerControlView CurrentDesignerControlView
        {
            get { return _editor.Visible ? DesignerControlView.Design : DesignerControlView.Source; }
            set
            {
                //switch between editor and markup view
                _editor.Visible = value == DesignerControlView.Design;
                _pnlMarkup.Visible = !_editor.Visible;

                _lnkBtnShowDesign.Font.Bold = _editor.Visible;
                _lnkBtnShowSource.Font.Bold = !_editor.Visible;
            }
        }

        void ToggleDesignSource()
        {
            CurrentDesignerControlView = CurrentDesignerControlView == DesignerControlView.Design
                                             ? DesignerControlView.Source
                                             : DesignerControlView.Design;
        }
    }
}
